//
//  XMVistorView.swift
//  XMWeibo-Swift
//
//  Created by 育鑫 王 on 15/10/27.
//  Copyright © 2015年 育鑫 王. All rights reserved.
//

import UIKit

//MARK - 协议

protocol XMVistorViewDelegate : NSObjectProtocol{
    func vistorViewRegistClick ()
    
    func vistorViewLoginClick ()

}


class XMVistorView: UIView {
    
    //协议属性
    weak var vistorViewDelegate: XMVistorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK - 按钮点击事件
    func loginBtnClick() {
        print(__FUNCTION__)
        vistorViewDelegate?.vistorViewLoginClick()
    }
    func registerClick() {
        print(__FUNCTION__)
        vistorViewDelegate?.vistorViewRegistClick()
    }
    
    //准备UI
    func prepareUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(descLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        descLabel.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        
        
        //添加约束
        //转轮 
        //center X
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //center Y
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //小房子
        //center X
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //center Y
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //详情描述
        //center X
        self.addConstraint(NSLayoutConstraint(item: descLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 16))
        //center Y
        self.addConstraint(NSLayoutConstraint(item: descLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        self.addConstraint(NSLayoutConstraint(item: descLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        //注册按钮
        //center X
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //center Y
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高度
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登录按钮
        //center X
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //center Y
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高度
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //遮盖图片
        //X
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        //Y
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //底部与register重合
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }
    //MARK - 懒加载b
    //转轮
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView()
        
        //设置图片
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        imageView.image = image
        
        imageView.sizeToFit()
        
        return imageView
    }()
    
    
    //home图片
    private lazy var homeView: UIImageView = {
        let imageView = UIImageView()
        
        //设置图片
        let image = UIImage(named: "visitordiscover_feed_image_house")
        imageView.image = image
        
        imageView.sizeToFit()
        
        return imageView
    }()
    
    //文字描述
    private lazy var descLabel: UILabel = {
        let label = UILabel()
        
        
        label.text = "关注一些人，看看有什么惊喜呀"
        label.textColor = UIColor.lightGrayColor()
        label.numberOfLines = 0
        
        
        return label
        }()
    
    //注册按钮
    private lazy var registerBtn: UIButton = {
        let registerBtn = UIButton()
        
//        registerBtn.imageView?.image = UIImage(named: "common_button_white_disable")
//        registerBtn.titleLabel?.text = "注册"
//        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击事件
        registerBtn.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        registerBtn.sizeToFit()
        return registerBtn
    }()
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
        let loginBtn = UIButton()
        
//        loginBtn.imageView?.image = UIImage(named: "common_button_white_disable")
//        loginBtn.titleLabel?.text = "登录"
        
                loginBtn.setTitle("登录", forState: UIControlState.Normal)
                loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //添加点击事件
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        loginBtn.sizeToFit()
        
        return loginBtn
    }()
    
    //遮盖图片
    private lazy var coverView: UIImageView = {
        let coverView = UIImageView()
        
        //设置图片
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        coverView.image = image
        
        coverView.sizeToFit()
        
        return coverView
        }()
    
    //设置背景描述
    func setUpInfo (imageName : String, message : String){
        iconView.image = UIImage(named: imageName)
        
        descLabel.text = message
        descLabel.numberOfLines = 0
        
        //隐藏小房子
        homeView.hidden = true
        
        //遮盖放置最下层
        sendSubviewToBack(coverView)
    }
    
    func startRotationAnimation (){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        
        //设置不停止
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
    }
    
    
}
